﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using CPlatePrintSys.Annotation.Events;
using CPlatePrintSys.Annotation.Models;
using Prism.Events;
using Prism.Mvvm;
using Prism.Commands;

namespace CPlatePrintSys.Annotation.ViewModels
{
    public class TextAnnotationEditViewModel : BindableBase
    {
        private readonly IEventAggregator _eventAggregator;
        private string _text;
        private string _fontFamily = "Arial";
        private double _fontSize = 10;
        private bool _underline;
        private Color _color = Colors.Red;
        private string _controlId;

        public DelegateCommand ApplyCommand { get; private set; }
        public DelegateCommand CancelCommand { get; private set; }
        public DelegateCommand DeleteCommand { get; private set; }
        
        public string ControlId 
        { 
            get => _controlId;
            set => SetProperty(ref _controlId, value);
        }

        public TextAnnotationEditViewModel() { }

        public TextAnnotationEditViewModel(IEventAggregator eventAggregator)
        {
            _eventAggregator = eventAggregator;
            
            // 初始化命令
            ApplyCommand = new DelegateCommand(ExecuteApply);
            CancelCommand = new DelegateCommand(ExecuteCancel);
            DeleteCommand = new DelegateCommand(ExecuteDelete);
            
            eventAggregator.GetEvent<ShowTextEditPanel>().Subscribe(OnShowTextEditPanel);
        }

        // 新增：设置ControlId的方法
        public void SetControlId(string controlId)
        {
            ControlId = controlId;
        }

        private void OnShowTextEditPanel(TextEdittingPanelInfo info)
        {
            // 使用传入的ControlId，而不是依赖自身的ControlId
            if (info?.TextInfo != null)
            {
                // 设置当前控件的ControlId
                this.ControlId = info.ControlId;
                
                Text = info.TextInfo.Text;
                FontSize = info.TextInfo.Height;
                Color = info.TextInfo.Color;
                Underline = info.TextInfo.Underline;
            }
        }

        private void ExecuteApply()
        {
            // 创建编辑信息
            var editInfo = new TextEditInfo
            {
                Text = Text,
                Color = Color,
                Height = FontSize,
                FontFamily = FontFamily,
                Underline = Underline
            };

            // 发布应用编辑事件
            _eventAggregator.GetEvent<ApplyTextEditEvent>().Publish(new ApplyTextEditEventArgs { ControlId = this.ControlId, EditInfo = editInfo });
        }

        private void ExecuteCancel()
        {
            // 发布取消编辑事件
            _eventAggregator.GetEvent<CancelTextEditEvent>().Publish(this.ControlId);
        }

        private void ExecuteDelete()
        {
            _eventAggregator.GetEvent<DeleteTextEditEvent>().Publish(this.ControlId);
        }

        public string Text
        {
            get => _text;
            set => SetProperty(ref _text, value);
        }

        public string FontFamily
        {
            get => _fontFamily;
            set => SetProperty(ref _fontFamily, value);
        }

        public double FontSize
        {
            get => _fontSize;
            set => SetProperty(ref _fontSize, value);
        }

        public bool Underline
        {
            get => _underline;
            set => SetProperty(ref _underline, value);
        }

        public Color Color
        {
            get => _color;
            set => SetProperty(ref _color, value);
        }

        public ObservableCollection<string> FontFamilies { get; } = new ObservableCollection<string>(
               Fonts.SystemFontFamilies.Select(f => f.Source));
        public ObservableCollection<double> FontSizes { get; } = new ObservableCollection<double> { 8, 9, 10, 11, 12, 14, 16, 18, 20, 24, 28, 32, 36, 48, 72 };

    }
}
